/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.huangjianbo.supos.openapi.core.thread;

import io.gitee.huangjianbo.supos.openapi.core.config.SuposApiClientConfig;
import io.gitee.huangjianbo.supos.openapi.core.config.SuposEndpoint;
import io.gitee.huangjianbo.supos.openapi.core.http.HttpLanguage;
import io.gitee.huangjianbo.supos.openapi.core.tools.StringUtils;

/**
 * 客户端上下文包装类
 * 
 * @author tomcat
 * @version 1.0
 */
public class ClientContextWrapper implements ClientContext {

    private final ClientContext context;
    private final SuposApiClientConfig globalConfig;

    public ClientContextWrapper(SuposApiClientConfig globalConfig) {
        this.context = ClientContextHolder.getContext();
        this.globalConfig = globalConfig;
    }

    @Override
    public void close() {
        ClientContextHolder.removeContext();
    }

    @Override
    public String getTicket() {
        if (StringUtils.isNotEmpty(this.context.getTicket())) {
            return this.context.getTicket();
        }
        return this.globalConfig.getTicket();
    }

    @Override
    public void setTicket(String ticket) {
        this.context.setTicket(ticket);
    }

    @Override
    public String getAccessKey() {
        if (StringUtils.isNotEmpty(this.context.getAccessKey())) {
            return this.context.getAccessKey();
        }
        return this.globalConfig.getAccessKey();
    }

    @Override
    public void setAccessKey(String accessKey) {
        this.context.setAccessKey(accessKey);
    }

    @Override
    public String getSecretKey() {
        if (StringUtils.isNotEmpty(this.context.getSecretKey())) {
            return this.context.getSecretKey();
        }
        return this.globalConfig.getSecretKey();
    }

    @Override
    public void setSecretKey(String secretKey) {
        this.context.setSecretKey(secretKey);
    }

    @Override
    public String getTenantId() {
        if (StringUtils.isNotEmpty(this.context.getTenantId())) {
            return this.context.getTenantId();
        }
        return this.globalConfig.getTenantId();
    }

    @Override
    public void setTenantId(String tenantId) {
        this.context.setTenantId(tenantId);
    }

    @Override
    public SuposEndpoint getEndpoint() {
        if (this.context.getEndpoint() != null) {
            this.context.getEndpoint().validate();
            return this.context.getEndpoint();
        }
        this.globalConfig.getEndpoint().validate();
        return this.globalConfig.getEndpoint();
    }

    @Override
    public void setEndpoint(SuposEndpoint endpoint) {
        this.context.setEndpoint(endpoint);
    }

    @Override
    public HttpLanguage getLanguage() {
        if (this.context.getLanguage() != null) {
            return this.context.getLanguage();
        }
        return this.globalConfig.getDefaultLanguage();
    }

    @Override
    public void setLanguage(HttpLanguage language) {
        this.context.setLanguage(language);
    }
}
